﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Tbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Tbl
{
    public class tblContactInfoDAL:ItblContactInfo
    {
        tblContactInfo info;
        HKEA_CMSEntities model;
        public tblContactInfoDAL()
        {
            model = new HKEA_CMSEntities();
        }

        public static tblContactInfoDAL GetInstance()
        {
            return new tblContactInfoDAL();
        }

        public int InsertENT(tblContactInfo ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.tblContactInfoes.AddObject(ent);
            if (model.SaveChanges() > 0)
                ent = model.tblContactInfoes.Where(a => a.ContactInfoUID > 0).OrderBy(a => a.ContactInfoUID).ToList().Last();
            return ent.ContactInfoUID;
        }



        public List<tblContactInfo> GetListByENT(tblContactInfo ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            
            var result = model.tblContactInfoes.Where(a => a.ContactInfoUID > 0);
            if (ent.ContactInfoUID > 0)
                result = result.Where(a => a.ContactInfoUID == ent.ContactInfoUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }

        public List<tblContactInfo> GetListByENT(tblContactInfo ent)
        {
            var model = DataAccess.GetModelInstance();
            var result = model.tblContactInfoes.Where(a => a.ContactInfoUID > 0);
            if (ent.ContactInfoUID > 0)
                result = result.Where(a => a.ContactInfoUID == ent.ContactInfoUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }

        public int GetCountByENT(tblContactInfo ent)
        {
            var model = DataAccess.GetModelInstance();
            var result = model.tblContactInfoes.Where(a => a.ContactInfoUID > 0);
            if (ent.ContactInfoUID > 0)
                result = result.Where(a => a.ContactInfoUID == ent.ContactInfoUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(tblContactInfo ent)
        {
            var model = DataAccess.GetModelInstance();
            info = model.tblContactInfoes.Where(a=>a.ContactInfoUID==ent.ContactInfoUID).FirstOrDefault();
            if (info != null)
            {
                info.LastModifyBy = ent.LastModifyBy;
                info.LastModifyDate = DateTime.Now;
                info.CountryCode = ent.CountryCode;
                info.Email1 = ent.Email1;
                info.Email1_IsSubscribe = ent.Email1_IsSubscribe;
                info.Email1_Type = ent.Email1_Type;
                info.Email1_HardBounce = ent.Email1_HardBounce;
                info.Email2 = ent.Email2;
                info.Email2_IsSubscribe = ent.Email2_IsSubscribe;
                info.Email2_Type = ent.Email2_Type;
                info.Email2_HardBounce = ent.Email2_HardBounce;
                info.Email3 = ent.Email3;
                info.Email3_IsSubscribe = ent.Email3_IsSubscribe;
                info.Email3_Type = ent.Email3_Type;
                info.Email3_HardBounce = ent.Email3_HardBounce;
                info.Email4 = ent.Email4;
                info.Email4_IsSubscribe = ent.Email4_IsSubscribe;
                info.Email4_Type = ent.Email4_Type;
                info.Email4_HardBounce = ent.Email4_HardBounce;
                info.Email5 = ent.Email5;
                info.Email5_IsSubscribe = ent.Email5_IsSubscribe;
                info.Email5_Type = ent.Email5_Type;
                info.Email5_HardBounce = ent.Email5_HardBounce;

                info.Fax1 = ent.Fax1;
                info.Fax1_AreaCode = ent.Fax1_AreaCode;
                info.Fax1_CountryCode = ent.Fax1_CountryCode;
                info.Fax1_IsDNC = ent.Fax1_IsDNC;
                info.Fax1_IsSubscribe = ent.Fax1_IsSubscribe;
                info.Fax1_Type = ent.Fax1_Type;
                info.Fax2 = ent.Fax2;
                info.Fax2_AreaCode = ent.Fax2_AreaCode;
                info.Fax2_CountryCode = ent.Fax2_CountryCode;
                info.Fax2_IsDNC = ent.Fax2_IsDNC;
                info.Fax2_IsSubscribe = ent.Fax2_IsSubscribe;
                info.Fax2_Type = ent.Fax2_Type;
                info.Fax3 = ent.Fax3;
                info.Fax3_AreaCode = ent.Fax3_AreaCode;
                info.Fax3_CountryCode = ent.Fax3_CountryCode;
                info.Fax3_IsDNC = ent.Fax3_IsDNC;
                info.Fax3_IsSubscribe = ent.Fax3_IsSubscribe;
                info.Fax3_Type = ent.Fax3_Type;
                info.Fax4 = ent.Fax4;
                info.Fax4_AreaCode = ent.Fax4_AreaCode;
                info.Fax4_CountryCode = ent.Fax4_CountryCode;
                info.Fax4_IsDNC = ent.Fax4_IsDNC;
                info.Fax4_IsSubscribe = ent.Fax4_IsSubscribe;
                info.Fax4_Type = ent.Fax4_Type;
                info.Fax5 = ent.Fax5;
                info.Fax5_AreaCode = ent.Fax5_AreaCode;
                info.Fax5_CountryCode = ent.Fax5_CountryCode;
                info.Fax5_IsDNC = ent.Fax5_IsDNC;
                info.Fax5_IsSubscribe = ent.Fax5_IsSubscribe;
                info.Fax5_Type = ent.Fax5_Type;

                info.Tel1 = ent.Tel1;
                info.Tel1_AreaCode = ent.Tel1_AreaCode;
                info.Tel1_CountryCode = ent.Tel1_CountryCode;
                info.Tel1_IsDNC = ent.Tel1_IsDNC;
                info.Tel1_IsSubscribe = ent.Tel1_IsSubscribe;
                info.Tel1_Type = ent.Tel1_Type;

                info.Tel2 = ent.Tel2;
                info.Tel2_AreaCode = ent.Tel2_AreaCode;
                info.Tel2_CountryCode = ent.Tel2_CountryCode;
                info.Tel2_IsDNC = ent.Tel2_IsDNC;
                info.Tel2_IsSubscribe = ent.Tel2_IsSubscribe;
                info.Tel2_Type = ent.Tel2_Type;
                info.Tel3 = ent.Tel3;
                info.Tel3_AreaCode = ent.Tel3_AreaCode;
                info.Tel3_CountryCode = ent.Tel3_CountryCode;
                info.Tel3_IsDNC = ent.Tel3_IsDNC;
                info.Tel3_IsSubscribe = ent.Tel3_IsSubscribe;
                info.Tel3_Type = ent.Tel3_Type;
                info.Tel4 = ent.Tel4;
                info.Tel4_AreaCode = ent.Tel4_AreaCode;
                info.Tel4_CountryCode = ent.Tel4_CountryCode;
                info.Tel4_IsDNC = ent.Tel4_IsDNC;
                info.Tel4_IsSubscribe = ent.Tel4_IsSubscribe;
                info.Tel4_Type = ent.Tel4_Type;
                info.Tel5 = ent.Tel5;
                info.Tel5_AreaCode = ent.Tel5_AreaCode;
                info.Tel5_CountryCode = ent.Tel5_CountryCode;
                info.Tel5_IsDNC = ent.Tel5_IsDNC;
                info.Tel5_IsSubscribe = ent.Tel5_IsSubscribe;
                info.Tel5_Type = ent.Tel5_Type;

                info.Website1 = ent.Website1;
                info.Website1_Type = ent.Website1_Type;
                info.Website2 = ent.Website2;
                info.Website2_Type = ent.Website2_Type;
                info.Website3 = ent.Website3;
                info.Website3_type = ent.Website3_type;
                info.Website4 = ent.Website4;
                info.Website4_type = ent.Website4_type;
                info.Website5 = ent.Website5;
                info.Website5_type = ent.Website5_type;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(tblContactInfo ent)
        {
            var model = DataAccess.GetModelInstance();
            var temp = model.tblContactInfoes.Where(a => a.ContactInfoUID > 0);
            if (ent.ContactInfoUID > 0)
                temp = temp.Where(a=>a.ContactInfoUID==ent.ContactInfoUID);
            var list = temp.ToList();
            for (int i = 0; i < list.Count(); i++)
            {
                model.tblContactInfoes.DeleteObject(list[i]);
            }
            return model.SaveChanges();
        }
    }
}
